class_name ChargeBehavior
extends Node

@export var enemy: Enemy
@export var anim_effects: AnimationPlayer
@export var pre_time := 1.0
@export var cooldown := 3.0

var is_charging := false
var current_cooldown := 0.0
var charge_attack_position := Vector2.ZERO

func _process(delta: float) -> void:
	if enemy == null or not is_instance_valid(Global.player):
		return
	if is_charging:
		enemy.global_position = enemy.global_position.move_toward(charge_attack_position, enemy.stats.speed * 5 * delta)
		if enemy.global_position.distance_to(charge_attack_position) < 50:
			end_charge()
	else:
		if current_cooldown > 0:
			current_cooldown -= delta
		elif enemy.can_move:
			start_charge()


func start_charge() -> void:
	enemy.can_move = false
	anim_effects.play("charge")
	await anim_effects.animation_finished
	is_charging = true
	charge_attack_position = Global.player.global_position

func end_charge() -> void:
	is_charging = false
	current_cooldown = cooldown
	enemy.can_move = true
